﻿using System;

namespace c4
{
    public class Node<T>{
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }



    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            head = tail = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action)
        {
            Node<T> current = head;
            while (current != null)
            {
                action(current.Data);
                current = current.Next;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> list = new GenericList<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);

            // 打印链表元素
            list.ForEach(data => Console.WriteLine(data));

            // 求最大值
            int max = int.MinValue;
            list.ForEach(data => { if (data > max) max = data; });
            Console.WriteLine($"Max: {max}");

            // 求最小值
            int min = int.MaxValue;
            list.ForEach(data => { if (data < min) min = data; });
            Console.WriteLine($"Min: {min}");

            // 求和
            int sum = 0;
            list.ForEach(data => sum += data);
            Console.WriteLine($"Sum: {sum}");
        }
    }
}
